import json

import elasticsearch7
import requests
from sqlalchemy import text

from RedisClient import RedisClient
from config.secure import ES_URI, USERNAME, PASSWORD, PORT
from models.base import db as samereportDB


class SearchES(object):
    server = ES_URI
    headers = {'content-type': 'application/json'}
    es = elasticsearch7.Elasticsearch([ES_URI], http_auth=(USERNAME, PASSWORD), port=PORT)
    scroll = ''

    def init(self):
        body = {
            "query": {
                "range": {
                    "time": {
                        "gte": 1659283200
                    }
                }
            }
        }
        res = self.es.search(index='internet', body=body, scroll='1m', size=100)
        self.scroll = res['_scroll_id']

    def scan(self):
        while True:
            body = {
                'scroll_id': self.scroll,
                'scroll': '1m'
            }
            res = requests.get(f'{ES_URI}:{PORT}/_search/scroll', data=json.dumps(body),
                               headers=self.headers, auth=(USERNAME, PASSWORD))
            if res.status_code == 200:
                data = json.loads(res.content)
                self.scroll = data['_scroll_id']
                if len(data['hits']['hits']) > 0:
                    for item in data['hits']['hits']:
                        _id = item['_id']
                        content = item['_source']['content']
                        count, zh = bot.count_zh(content)
                        if count <= 100:
                            print(_id)
                            self.delete(_id)
                else:
                    return True

    def delete(self, _id):
        searchbody = {
            'query': {
                'term': {
                    '_id': _id
                }
            }
        }
        searchResponse = requests.post(
            f'{ES_URI}:{PORT}/internet/_delete_by_query',
            headers=self.headers,
            data=json.dumps(searchbody),
            auth=(USERNAME, PASSWORD)
        )
        print(searchResponse)

    def phrase(self, content):
        try:
            url = content.split('百度和网页')[1].split('的作者无关')[0]
            return url
        except:
            return None

    def update_url(self, snap_url, url):
        searchbody = {
            'query': {
                'match_phrase': {
                    'url': snap_url
                }
            }
        }
        searchResponse = requests.get(
            f'{ES_URI}:{PORT}/internet/_search',
            headers=self.headers,
            data=json.dumps(searchbody),
            auth=(USERNAME, PASSWORD)
        )
        if searchResponse.status_code == 200:
            data = json.loads(searchResponse.content)
            if len(data['hits']['hits']) > 0:
                for item in data['hits']['hits']:
                    uid = item['_id']
                    update_body = {
                        'doc': {
                            'url': url
                        }
                    }
                    updateResponse = requests.post(
                        f'{ES_URI}:{PORT}/internet/_doc/{uid}/_update',
                        headers=self.headers,
                        data=json.dumps(update_body),
                        auth=(USERNAME, PASSWORD)
                    )
                    print(updateResponse.status_code)

    def insert_to_db(self, uid, title, time):
        try:
            sql = f"insert into internet_data (uid, title, time) VALUE (:uid,:title,:time)"
            with self.db.auto_commit():
                self.db.session.execute(text(sql), {
                    'uid': uid,
                    'title': title,
                    'time': time
                })
        except:
            pass

    def delete_same(self, title, time):
        body = {
            'query': {
                "bool": {
                    "must": [
                        {
                            "term": {
                                "title": title
                            }
                        }
                    ],
                    "must_not": [
                        {
                            "term": {
                                "time": time
                            }
                        }
                    ]
                }
            }
        }
        deleteResponse = requests.post(f'{ES_URI}:{PORT}/internet/_delete_by_query', headers=self.headers,
                                       data=json.dumps(body), auth=(USERNAME, PASSWORD))
        print(deleteResponse.status_code)

    def count_zh(self, paragraph):
        count = 0
        zh = ''
        for s in paragraph:
            if '\u4e00' <= s <= '\u9fa5':
                count += 1
                zh += s
        return count, zh


if __name__ == '__main__':
    bot = SearchES()
    bot.init()
    bot.scan()

